package com.hc.controller;


import com.github.pagehelper.PageInfo;
import com.hc.entity.RRepairSetting;
import com.hc.service.IRepairSettingService;
import com.hc.vo.AddRepairSettingVO;
import com.hc.vo.StatusVO;
import com.hc.vo.GetRepairSettingVO;
import com.hc.vo.PagingVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setting")
public class RepairSettingController {

    IRepairSettingService service;

    @Autowired
    public void setService(IRepairSettingService service) {
        this.service = service;
    }

    @GetMapping("/{ID}")
    public StatusVO getOne(@PathVariable String ID) {
        RRepairSetting setting = service.getOne(ID);
        return new StatusVO(setting);
    }

    @PostMapping("/{ID}")
    //修改的字段和添加时的字段是完全一至的
    public StatusVO updateById(@PathVariable String ID, @Valid @RequestBody AddRepairSettingVO updateVO) {
        RRepairSetting setting = new RRepairSetting();
        BeanUtils.copyProperties(updateVO, setting);
        setting.setSettingId(ID);
        service.updateSetting(setting);
        return new StatusVO(201);
    }

    @GetMapping
    @ApiOperation(value = "根据条件查询保修设置", notes = "返回类型实际上是PagingVO的类型", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE, response = GetRepairSettingVO.class)
    public StatusVO getAll(@RequestParam(required = false, defaultValue = "-1") int pageNum,
                           @RequestParam(required = false, defaultValue = "-1") int pageSize,
                           @RequestParam(required = false) String typeName,
                           @RequestParam(required = false) String repairWay) {
        if (pageNum == -1 || pageSize == -1) {
            List<RRepairSetting> data = service.getAll();
            return new StatusVO(data);
        } else {
            PageInfo<RRepairSetting> pageInfo = service.getPaged(pageNum, pageSize, typeName, repairWay);
            PagingVO<GetRepairSettingVO> result = new PagingVO<>();
            result.setData(pageInfo.getList().stream().map(
                    (ent) -> {
                        GetRepairSettingVO vo = new GetRepairSettingVO();
                        BeanUtils.copyProperties(ent, vo);
                        return vo;
                    }
            ).collect(Collectors.toList()));
            result.setMaxRows(pageInfo.getTotal());
            return new StatusVO(result);
        }
    }

    @PostMapping
    @ApiOperation(value = "添加报修设置", code = 201, httpMethod = "POST", consumes = MediaType.APPLICATION_JSON_VALUE)
    public StatusVO add(@RequestBody @Valid AddRepairSettingVO vo, Errors errors) {
        System.out.println("success");
        if (errors.hasErrors()) {
            return new StatusVO(400, "填入的内容错误");
        }
        RRepairSetting rRepairSetting = new RRepairSetting();
        BeanUtils.copyProperties(vo, rRepairSetting);
        service.addSetting(rRepairSetting);
        return new StatusVO(201, "成功");
    }

    @DeleteMapping
    @ApiOperation(value = "删除报修配置", code = 204, produces = MediaType.APPLICATION_JSON_VALUE, response = StatusVO.class)
    public StatusVO delete(@RequestParam String settingId) {
        service.deleteSetting(settingId);
        return new StatusVO(204, "删除成功");
    }

}
